package com.mall.b2c.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.b2c.dao.DeliveryCenterDao;
import com.mall.b2c.domain.delivery.DeliveryCenterDomain;
import com.mall.b2c.service.DeliveryCenterService;
import com.mall.common.domain.AreaDomain;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.AreaService;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 发货点
 * @author lzx
 */
@Service("b2cDeliveryCenterService")
@Transactional(readOnly=true)
public class DeliveryCenterServiceImpl extends BaseServiceImpl<DeliveryCenterDomain, Long> implements DeliveryCenterService {
	
	@Resource private AreaService areaService;
	
	@Resource
	public void setBaseDao(DeliveryCenterDao deliveryCenterDao) { 
		super.setBaseDao(deliveryCenterDao); 
	}

	@Override
	public DeliveryCenterDomain findDefault() {
		Finder finder = Finder.create("from DeliveryCenterDomain d where d.isDefault = :isDefault");
		finder.setParam("isDefault", true);
		return (DeliveryCenterDomain) finder.findUniqueByHQL();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(DeliveryCenterDomain deliveryCenter, Long areaId) {
		if(!this.isUnique("name", null, deliveryCenter.getName())) {
			throw new NoticeException("该发货点已存在");
		}
		
		AreaDomain area = areaService.find(areaId);
		if(area != null){
			deliveryCenter.setAreaId(areaId);
			deliveryCenter.setDistric(area.getFull_name());
		}
		
		if(deliveryCenter.getIsDefault() != null && deliveryCenter.getIsDefault()){
			DeliveryCenterDomain defaultDeliveryCenter = findDefault();
			if(defaultDeliveryCenter != null){
				defaultDeliveryCenter.setIsDefault(false);
				this.update(defaultDeliveryCenter);
			}
		}
		
		this.persist(deliveryCenter);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void edit(DeliveryCenterDomain deliveryCenter, Long areaId) {
		DeliveryCenterDomain currentDeliveryCenter = this.find(deliveryCenter.getId());
		if(currentDeliveryCenter == null) {
			throw new NoticeException("当前发货点不存在!");
		}
		if(!this.isUnique("name", currentDeliveryCenter.getName(), deliveryCenter.getName())) {
			throw new NoticeException("发货点名称已存在!");
		}
		AreaDomain area = areaService.find(areaId);
		if(area != null){
			deliveryCenter.setAreaId(areaId);
			deliveryCenter.setDistric(area.getFull_name());
		}
		
		if(deliveryCenter.getIsDefault() != null && deliveryCenter.getIsDefault()){
			DeliveryCenterDomain defaultDeliveryCenter = findDefault();
			if(defaultDeliveryCenter != null){
				defaultDeliveryCenter.setIsDefault(false);
				this.update(defaultDeliveryCenter);
			}
		}else{
			deliveryCenter.setIsDefault(false);
		}
		
		this.updateByUpdater(deliveryCenter);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void switchDefault(Long newId) {
		DeliveryCenterDomain newDeliveryCenter = this.find(newId);
		if(newDeliveryCenter == null){
			throw new NoticeException("该发货点不存在!");
		}
		if(!newDeliveryCenter.getIsDefault()){
			DeliveryCenterDomain defaultDeliveryCenter = findDefault();
			if(defaultDeliveryCenter != null){
				defaultDeliveryCenter.setIsDefault(false);
				this.update(defaultDeliveryCenter);
			}
			newDeliveryCenter.setIsDefault(true);
		}else{
			newDeliveryCenter.setIsDefault(false);
		}
		this.update(newDeliveryCenter);
	}

}
